package com.mycity.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.mycity.bean.Comment;
import com.mycity.bean.Gamer;
import com.mycity.bean.Post;
import com.mycity.gamification.LevelCalculator;
import com.mycity.mapper.CommentRowMapper;
import com.mycity.mapper.PostRowMapper;
import com.mysql.jdbc.PreparedStatement;

public class CommentDao extends RDSDao{

	GameDao gameDao = new GameDao();

	public int commentOnPost(final String commentContent, final String commentDate, final int userId, final int postId){
		final String insertSQL = "INSERT INTO Comment(commentContent, commentDate, userId, postId, hasStar) VALUES(?,?,?,?,0)";

		KeyHolder holder = new GeneratedKeyHolder();

		this.getTemplate().update(new PreparedStatementCreator() {           


			@Override
			public java.sql.PreparedStatement createPreparedStatement(
					Connection con) throws SQLException {
				PreparedStatement ps = (PreparedStatement) con.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);
				ps.setString(1, commentContent);
				ps.setString(2, commentDate);
				ps.setInt(3, userId);
				ps.setInt(4, postId);
				return ps;
			}
		}, holder);

		int commentId = holder.getKey().intValue();

		Gamer gamer = gameDao.getGamer(userId);		
		gamer.setNumComments(gamer.getNumComments()+1);
		gamer.setExperience(LevelCalculator.updateExp(LevelCalculator.Objects.COMMENT, gamer.getExperience(), 1));
		gameDao.updateGamer(gamer);

		return commentId;

	}

	public List<Comment> getCommentsOfPost(int postId){
		Object params[] = { postId };
		List<Comment> comments = this.getTemplate().query("SELECT * from Comment where postId=? ORDER BY commentId ASC", params, new CommentRowMapper()); 

		return comments;
	}

	public void giveStar(int commentId){
		this.getTemplate().update("UPDATE Comment SET hasStar=1 WHERE commentId="+commentId);

		int userId = this.getTemplate().queryForInt("SELECT userId FROM Comment WHERE commentId="+commentId);

		Gamer gamer = gameDao.getGamer(userId);		
		gamer.setNumStars(gamer.getNumStars()+1);
		gamer.setExperience(LevelCalculator.updateExp(LevelCalculator.Objects.STAR, gamer.getExperience(), 1));
		gameDao.updateGamer(gamer);

	}

	public void removeStar(int commentId){
		int hasStar = this.getTemplate().queryForInt("SELECT hasStar FROM Comment WHERE commentId="+commentId);

		if(hasStar==1){
			this.getTemplate().update("UPDATE Comment SET hasStar=0 WHERE commentId="+commentId);
			int userId = this.getTemplate().queryForInt("SELECT userId FROM Comment WHERE commentId="+commentId);
			Gamer gamer = gameDao.getGamer(userId);		
			gamer.setNumStars(gamer.getNumStars()-1);
			gamer.setExperience(LevelCalculator.updateExp(LevelCalculator.Objects.STAR, gamer.getExperience(), -1));
			gameDao.updateGamer(gamer);
		}

	}

	public void deleteComment(int commentId){	
		int userId = this.getTemplate().queryForInt("SELECT userId FROM Comment WHERE commentId="+commentId);
		Gamer gamer = gameDao.getGamer(userId);		
		gamer.setNumComments(gamer.getNumComments()-1);
		gamer.setExperience(LevelCalculator.updateExp(LevelCalculator.Objects.COMMENT, gamer.getExperience(), -1));
		gameDao.updateGamer(gamer);

		Object params[] = { commentId };
		this.getTemplate().update("DELETE FROM Comment WHERE commentId=?", params);
	}

}
